﻿using Dapper;
using System;
using BCEntity.MakeStaticPagingComponent.PageTemplateAttachment;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.MakeStaticPagingComponent.WebsiteTemplateAttachment;

namespace BCData.MakeStaticPagingComponent.WebsiteTemplateAttachment
{
    /// <summary>
    /// 网站模板附件
    /// </summary>
	public class WebsiteTemplateAttachmentData : IWebsiteTemplateAttachmentData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitetemplateattachment";
        private const string QUERY = @"SELECT WebsiteTemplateAttachmentId,EditAdminId,EditAdminName,WebsiteTemplateId,FileType,InsertLocation,FileAddress,CreateAdminId,CreateAdminName,CreateTime,UpdateTime FROM websitetemplateattachment";
        public WebsiteTemplateAttachmentData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查网站模板附件是否存在
        public async Task<bool> Exists(int websiteTemplateAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteTemplateAttachmentId=?WebsiteTemplateAttachmentId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateAttachmentId", websiteTemplateAttachmentId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加网站模板附件
        public async Task<WebsiteTemplateAttachmentEntity> Add(WebsiteTemplateAttachmentEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitetemplateattachment", new string[] { "WebsiteTemplateId", "FileType", "InsertLocation", "FileAddress", "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE WebsiteTemplateAttachmentId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateId", entity.WebsiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?FileType", entity.FileType.ToString(), DbType.AnsiString);
            parameters.Add("?InsertLocation", entity.InsertLocation.ToString(), DbType.AnsiString);
            parameters.Add("?FileAddress", entity.FileAddress?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteTemplateAttachmentEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改网站模板附件
        public async Task<WebsiteTemplateAttachmentEntity> Update(WebsiteTemplateAttachmentEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitetemplateattachment", new string[] { "EditAdminId", "EditAdminName", "WebsiteTemplateId", "FileType", "InsertLocation", "FileAddress" }, new string[] { "WebsiteTemplateAttachmentId" });
            sql = string.Format("{0} {1} WHERE WebsiteTemplateAttachmentId=?WebsiteTemplateAttachmentId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateAttachmentId", entity.WebsiteTemplateAttachmentId, DbType.Int32, size: 11);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?WebsiteTemplateId", entity.WebsiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?FileType", entity.FileType.ToString(), DbType.AnsiString);
            parameters.Add("?InsertLocation", entity.InsertLocation.ToString(), DbType.AnsiString);
            parameters.Add("?FileAddress", entity.FileAddress?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteTemplateAttachmentEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除网站模板附件
        public async Task<bool> Delete(int websiteTemplateAttachmentId)
        {
            string sql = @"DELETE FROM websitetemplateattachment WHERE WebsiteTemplateAttachmentId=?WebsiteTemplateAttachmentId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateAttachmentId", websiteTemplateAttachmentId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取网站模板附件
        public async Task<WebsiteTemplateAttachmentEntity> GetEntity(int websiteTemplateAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteTemplateAttachmentId=?WebsiteTemplateAttachmentId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateAttachmentId", websiteTemplateAttachmentId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteTemplateAttachmentEntity>(sql, parameters);
        }
        #endregion

        #region 获取网站模板附件列表
        public async Task<IEnumerable<WebsiteTemplateAttachmentEntity>> GetList(int websiteTemplateId)
        {
            string sql = string.Format("{0} WHERE WebsiteTemplateId=?WebsiteTemplateId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteTemplateId", websiteTemplateId, DbType.Int32, size: 11);
            return await this.databaseContext.Slave.QueryAsync<WebsiteTemplateAttachmentEntity>(sql, parameters);
        }
        #endregion

        #region 获取网站模板附件分页
        public async Task<(IEnumerable<WebsiteTemplateAttachmentEntity>, int)> Query(int websiteTemplateId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("WebsiteTemplateId=?WebsiteTemplateId");
            parameters.Add("?WebsiteTemplateId", websiteTemplateId, DbType.Int32, size: 11);
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitetemplateattachment {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteTemplateAttachmentEntity, int>(sql, parameters);
        }
        #endregion
    }
}



